/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Guis.Categoria;

import Clases.Categoria;
import Controlador.ControladorCategoria;
import static Guis.Principal.Principal.jDesktopPane1;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.InputMap;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

/**
 *
 * @author Angelo
 */
public class ModificarCategoria extends javax.swing.JDialog {

    /**
     * Creates new form ModificarCategoria
     */
    private ControladorCategoria cc;
    private Categoria cat;

    public ModificarCategoria(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        cc = new ControladorCategoria();
        /**/
        SoloLetras(jTextArea1);
        SoloLetras(jTextField1);
        BloquearCopiar(jTextField1);
        BloquearCopiar(jTextArea1);
        MaximoCaracteres45(jTextArea1);
        MaximoCaracteres45(jTextField1);
        /**/
        jTextArea1.setLineWrap(true);
        //********************************************************************//
        this.setResizable(false);
        this.setTitle("Modificar categoría");
        Dimension desktopSize = jDesktopPane1.getSize();
        Dimension jInternalFrameSize = this.getSize();
        this.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                (desktopSize.height - jInternalFrameSize.height) / 2);
    }

    private void SoloLetras(JTextArea a) {
        a.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (Character.isDigit(c)) {
                    getToolkit().beep();
                    e.consume();
                }
            }
        });
    }

    private void SoloLetras(JTextField a) {
        a.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (Character.isDigit(c)) {
                    getToolkit().beep();
                    e.consume();
                }
            }
        });
    }

    private void BloquearCopiar(JTextField a) {
        // BLOQUEA LA ACCION COPIAR 
        InputMap map2 = a.getInputMap(JTextField.WHEN_FOCUSED);
        map2.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK), "null");
    }

    private void BloquearCopiar(JTextArea a) {
        // BLOQUEA LA ACCION COPIAR 
        InputMap map2 = a.getInputMap(JTextArea.WHEN_FOCUSED);
        map2.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK), "null");
    }

    private void MaximoCaracteres45(JTextField a) {
        a.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                JTextField a = (JTextField) e.getSource();
                if (a.getText().length() == 45) {
                    getToolkit().beep();
                    e.consume();
                }
            }
        });
    }

    private void MaximoCaracteres45(JTextArea a) {
        a.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                JTextArea a = (JTextArea) e.getSource();
                if (a.getText().length() == 45) {
                    getToolkit().beep();
                    e.consume();
                }
            }
        });
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Nombre:");

        jLabel2.setText("Descripción:");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("Guardar cambios");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jTextField1))
                        .addComponent(jLabel2)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        guardar();
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

    private void guardar() {
        if (validar()) {
            Categoria cat = new Categoria();
            cat.setNombre(jTextField1.getText());
            cat.setDescripcion(jTextArea1.getText());
            cat.setId_Categoria(this.cat.getId_Categoria());
            if (cc.modificarCategoria(cat)) {
                JOptionPane.showMessageDialog(rootPane, "Categoría modificada");
                dispose();
            } else {
                JOptionPane.showMessageDialog(rootPane, "No se pudo modificar la categoría");
            }
        }
    }

    public void setIdCategoria(int id) {
        cat = cc.getCategoria(id);
        jTextField1.setText(cat.getNombre());
        jTextArea1.setText(cat.getDescripcion());
        
    }

    private boolean validar() {

        boolean flag = true;

        if (this.jTextArea1.getText().length() == 0) {
            this.jLabel2.setForeground(Color.red);
            jTextArea1.grabFocus();
            flag = false;
        } else {
            jLabel2.setForeground(Color.black);
        }
        if (this.jTextField1.getText().length() == 0) {
            this.jLabel1.setForeground(Color.red);
            jTextField1.grabFocus();
            flag = false;
        } else {
            jLabel1.setForeground(Color.black);
        }
        return flag;
    }
}
